package com.cooper.mediadb.online;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.util.Log;

public final class MediaSrcMgr {

	public static final int SRC_LETV = 1;
	public static final int SRC_LETV_DOWN_ORDER = 101;
	public static final int SRC_LETV_PLAY_ORDER = 2;

	public static final int SRC_M1905 = 2;
	public static final int SRC_M1905_DOWN_ORDER = 98;
	public static final int SRC_M1905_PLAY_ORDER = 98;

	public static final int SRC_PPTV = 3;
	public static final int SRC_PPTV_DOWN_ORDER = 99;
	public static final int SRC_PPTV_PLAY_ORDER = 99;

	public static final int SRC_QIYI = 4;
	public static final int SRC_QIYI_DOWN_ORDER = 97;
	public static final int SRC_QIYI_PLAY_ORDER = 97;

	public static final int SRC_QQ = 5;
	public static final int SRC_QQ_DOWN_ORDER = 1;
	public static final int SRC_QQ_PLAY_ORDER = 1;

	public static final int SRC_SINA = 6;
	public static final int SRC_SINA_DOWN_ORDER = 95;
	public static final int SRC_SINA_PLAY_ORDER = 95;

	public static final int SRC_SOHU = 7;
	public static final int SRC_SOHU_DOWN_ORDER = 94;
	public static final int SRC_SOHU_PLAY_ORDER = 101;

	public static final int SRC_TUDOU = 8;
	public static final int SRC_TUDOU_DOWN_ORDER = 93;
	public static final int SRC_TUDOU_PLAY_ORDER = 93;

	public static final int SRC_YOUKU = 9;
	public static final int SRC_YOUKU_DOWN_ORDER = 92;
	public static final int SRC_YOUKU_PLAY_ORDER = 102;

	public static final int SRC_MTIME = 10;
	public static final int SRC_MTIME_DOWN_ORDER = 2;
	public static final int SRC_MTIME_PLAY_ORDER = 3;

	public static final int SRC_YUGAOPIAN = 101;

	public static final int GET_SOURCE_BY_DOWNLOAD = 1;
	public static final int GET_SOURCE_BY_PLAY = 2;
	public static final int GET_SOURCE_ALL = 3;

	public static final int SRC_UNKNOW = 99;
	public static final int SRC_UNKNOW_DOWN_ORDER = 102;
	public static final int SRC_UNKNOW_PLAY_ORDER = 103;

	public static final int SRC_FUNSHION = 11;
	public static final int SRC_FUNSHION_DOWN_ORDER = 90;
	public static final int SRC_FUNSHION_PLAY_ORDER = 90;

	public static final int SRC_XUNLEI = 12;
	public static final int SRC_XUNLEI_DOWN_ORDER = 89;
	public static final int SRC_XUNLEI_PLAY_ORDER = 89;

	public static final int SRC_PPS = 13;
	public static final int SRC_PPS_DOWN_ORDER = 96;
	public static final int SRC_PPS_PLAY_ORDER = 96;

	public static final int SRC_WASU = 14;
	public static final int SRC_WASU_DOWN_ORDER = 95;
	public static final int SRC_WASU_PLAY_ORDER = 95;

	public static final int SRC_CNTV = 15;
	public static final int SRC_CNTV_DOWN_ORDER = 94;
	public static final int SRC_CNTV_PLAY_ORDER = 94;

	public static final int SRC_V61 = 16;
	public static final int SRC_V61_DOWN_ORDER = 92;
	public static final int SRC_V61_PLAY_ORDER = 92;

	public static final int SRC_56 = 17;
	public static final int SRC_56_DOWN_ORDER = 91;
	public static final int SRC_56_PLAY_ORDER = 91;

	public static final String FORMAT_SUPER = "super"; // 1080 超清
	// public static final String FORMAT_SUPER2 = "super2"; //720 标清
	public static final String FORMAT_HIGH = "high"; // 720p 高清
	public static final String FORMAT_FLUENT = "fluent"; // 标清

	public static final String FORMAT_REAL = "real"; // 原版 流畅
	public static final String FORMAT_NORMAL = "normal"; // 流畅

	private static final String YOUKU_REG = "http:\\/\\/v\\.youku\\.com.*";
	private static final String LETV_REG = "http:\\/\\/www\\.letv\\.com.*";
	private static final String M1905_REG = "http:\\/\\/www\\.m1905\\.com.*";
	private static final String PPTV_REG = "http:\\/\\/v\\.pptv\\.com.*";

	private static final String FUNSHION_REG = "http:\\/\\/www\\.funshion\\.com.*";

	private static final String XUNLEI_REG = "http:\\/\\/vod\\.kankan\\.com.*";

	private static final String QIYI_REG = "http:\\/\\/www\\.iqiyi\\.com.*";
	private static final String QQ_REG = "http:\\/\\/v\\.qq\\.com.*";
	private static final String SINA_REG = "http:\\/\\/video\\.sina\\.com\\.cn.*";
	private static final String SOHU_REG = "http:\\/\\/tv\\.sohu\\.com.*";
	private static final String TUDOU_REG = "http:\\/\\/www\\.tudou\\.com.*";
	private static final String YUGAOPIAN_REG = "http:\\/\\/www\\.yugaopian\\.com\\/show.*";
	private static final String MTIME_REG = "http:\\/\\/.*?\\.mtime\\..*";

	private static final String WASU_REG = "http:\\/\\/www\\.wasu\\.cn.*";
	private static final String PPS_REG = "http:\\/\\/v\\.pps\\.tv.*";

	private static final String V56_REG = "http:\\/\\/www\\.56\\.com.*";
	private static final String CNTV_REG = "http:\\/\\/.*?\\.cntv\\.cn.*";
	private static final String V61_REG = "http:\\/\\/.*?\\.v\\.61\\.com.*";

	public static String getDownFormat(String format) {
		if (FORMAT_SUPER.equals(format)) {
			return FORMAT_HIGH;
		} else if (FORMAT_HIGH.equals(format)) {
			return FORMAT_FLUENT;
		} else if (FORMAT_FLUENT.equals(format)) {
			return FORMAT_REAL;
		} else if (FORMAT_REAL.equals(format)) {
			return FORMAT_NORMAL;
		} else
			return null;
		/*
		 * 
		 * if (FORMAT_SUPER.equals(format)) { return FORMAT_HIGH; } else if
		 * (FORMAT_HIGH.equals(format)) { return FORMAT_REAL; } else if
		 * (FORMAT_REAL.equals(format)) { return FORMAT_NORMAL; } else return
		 * null;
		 */
	}

	public static String getUpFormat(String format) {
		if (FORMAT_NORMAL.equals(format)) {
			return FORMAT_REAL;
		}else if (FORMAT_REAL.equals(format)){
			return FORMAT_FLUENT;
		} else if (FORMAT_FLUENT.equals(format)) {
			return FORMAT_HIGH;
		} else if (FORMAT_HIGH.equals(format)) {
			return FORMAT_SUPER;
		} else
			return null;
		/*
		 * if (FORMAT_NORMAL.equals(format)) { return FORMAT_REAL; } else if
		 * (FORMAT_REAL.equals(format)) { return FORMAT_HIGH; } else if
		 * (FORMAT_HIGH.equals(format)) { return FORMAT_SUPER; } else return
		 * null;
		 */
	}

	/*
	 * public List<Integer> getSrcs(List<String> urls) { List<Integer> result =
	 * new ArrayList<Integer>(); for (String url : urls) {
	 * result.add(analyzeSrc(url)); } return result; }
	 * 
	 * public String getSrcUrl(List<String> urls ,int src){ for (String url :
	 * urls){ if (src == analyzeSrc(url)){ return url; } } return null; }
	 */

	private static int analyzeSrc(String url) {

		if (url.matches(YOUKU_REG)) {
			return SRC_YOUKU;
		} else if (url.matches(LETV_REG)) {
			return SRC_LETV;
		} else if (url.matches(M1905_REG)) {
			return SRC_M1905;
		} else if (url.matches(PPTV_REG)) {
			return SRC_PPTV;
		} else if (url.matches(QIYI_REG)) {
			return SRC_QIYI;
		} else if (url.matches(QQ_REG)) {
			return SRC_QQ;
		} else if (url.matches(SINA_REG)) {
			return SRC_SINA;
		} else if (url.matches(SOHU_REG)) {
			return SRC_SOHU;
		} else if (url.matches(TUDOU_REG)) {
			return SRC_TUDOU;
		} else if (url.matches(YUGAOPIAN_REG)) {
			return SRC_YUGAOPIAN;
		} else if (url.matches(MTIME_REG)) {
			return SRC_MTIME;
		} else if (url.matches(FUNSHION_REG)) {
			return SRC_FUNSHION;
		} else if (url.matches(XUNLEI_REG)) {
			return SRC_XUNLEI;
		} else if (url.matches(WASU_REG)) {
			return SRC_WASU;
		} else if (url.matches(PPS_REG)) {
			return SRC_PPS;
		} else if (url.matches(V56_REG)) {
			return SRC_56;
		} else if (url.matches(V61_REG)) {
			return SRC_V61;
		} else if (url.matches(CNTV_REG)) {
			return SRC_CNTV;
		} else {
			Log.w("MovieSrcMgr", "url src is not found:" + url);
			return SRC_UNKNOW;
		}
	}

	private static int getSrcOrder(int src, int useType) {
		switch (src) {
		case SRC_LETV:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_LETV_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_LETV_PLAY_ORDER;
			} else
				return 1;
		case SRC_M1905:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_M1905_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_M1905_PLAY_ORDER;
			} else
				return 1;

		case SRC_PPTV:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_PPTV_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_PPTV_PLAY_ORDER;
			} else
				return 1;
		case SRC_QIYI:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_QIYI_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_QIYI_PLAY_ORDER;
			} else
				return 1;
		case SRC_QQ:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_QQ_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_QQ_PLAY_ORDER;
			} else
				return 1;
		case SRC_SINA:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_SINA_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_SINA_PLAY_ORDER;
			} else
				return 1;
		case SRC_SOHU:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_SOHU_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_SOHU_PLAY_ORDER;
			} else
				return 1;
		case SRC_TUDOU:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_TUDOU_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_TUDOU_PLAY_ORDER;
			} else
				return 1;
		case SRC_YOUKU:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_YOUKU_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_YOUKU_PLAY_ORDER;
			} else
				return 1;
		case SRC_MTIME:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_MTIME_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_MTIME_PLAY_ORDER;
			} else
				return 1;
		case SRC_UNKNOW:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_UNKNOW_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_UNKNOW_PLAY_ORDER;
			} else
				return 1;
		case SRC_XUNLEI:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_XUNLEI_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_XUNLEI_PLAY_ORDER;
			} else
				return 1;
		case SRC_FUNSHION:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_FUNSHION_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_FUNSHION_PLAY_ORDER;
			} else
				return 1;
		case SRC_PPS:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_PPS_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_PPS_PLAY_ORDER;
			} else
				return 1;
		case SRC_WASU:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_WASU_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_WASU_PLAY_ORDER;
			} else
				return 1;
		case SRC_56:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_56_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_56_PLAY_ORDER;
			} else
				return 1;
		case SRC_V61:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_V61_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_V61_PLAY_ORDER;
			} else
				return 1;
		case SRC_CNTV:
			if (useType == GET_SOURCE_BY_DOWNLOAD) {
				return SRC_CNTV_DOWN_ORDER;
			} else if (useType == GET_SOURCE_BY_PLAY) {
				return SRC_CNTV_PLAY_ORDER;
			} else
				return 1;
		}
		return 1;
	}

	public static boolean containsSrc(Collection<String> urls, int src) {
		for (String url : urls) {
			if (analyzeSrc(url) == src) {
				return true;
			}
		}
		return false;
	}

	public static List<MediaSource> getMediaSources(Map<String, String> urls,
			int useType) {
		List<MediaSource> result = new ArrayList<MediaSource>();
		for (Entry<String, String> entry : urls.entrySet()) {
			int src = analyzeSrc(entry.getKey());
			result.add(new MediaSource(entry.getKey(), entry.getValue(), src,
					getSrcOrder(src, useType)));
		}

		Collections.sort(result);
		if ((result.size() > 1) && (useType != GET_SOURCE_ALL)) {
			for (int i = result.size() - 1; i >= 0; i--) {
				if ((result.size() > 1) && (result.get(i).getOrder() > 100)) {
					result.remove(i);
				} else
					break;
			}
		}

		return result;
	}

}
